/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.krossk.dao;

import br.com.krossk.jpa.JPAFactory;
import br.com.krossk.model.Segmento;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
/**
 *
 * @author Henrique
 */
public class SegmentoDAO {
    private static SegmentoDAO instance = new SegmentoDAO();

    public static SegmentoDAO getInstance(){
        return instance;
    }

    public List <Segmento> getAll(){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            Query query = em.createNamedQuery("Segmento.findAll");
            return query.getResultList();
        }finally{
            em.close();
        }
    }

    public Segmento getById(int idSegmento){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            return em.find(Segmento.class, idSegmento);
        }finally{
            em.close();
        }

    }

    public void save(Segmento a){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            em.getTransaction().begin();
            em.persist(a);
            em.getTransaction().commit();
        }finally{
            em.close();
        }
    }

    public void delete(int idSegmento){
        EntityManager em = JPAFactory.getEntityManager();
        try{
            Segmento a = em.find(Segmento.class, idSegmento);
            em.getTransaction().begin();
            em.remove(a);
            em.getTransaction().commit();
        }finally{
            em.close();
        }
    }
}
